#include <bits/stdc++.h>

const int MAXN = 4e5 + 5;
using namespace std;

struct BIT {
#define lowbit(x) ((x) & (-(x)))
  int tree[MAXN];

  void add(int p, int x) {
    // a[p] = max(a[p],x);
    while (p) {
      tree[p] = max(tree[p], x);
      p -= lowbit(p);
    }
  }

  int query(int p) {
    int res = 0;
    while (p < MAXN) {
      res = max(res, tree[p]);
      p += lowbit(p);
    }
    return res;
  }
} bit;

vector<pair<int, int>> vec;
int n;
int ans[MAXN];

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int a, b;
    cin >> a >> b;
    if (a > b)
      swap(a, b);
    vec.push_back({b, a});
  }
  sort(vec.begin(), vec.end());

  for (int i = 0; i < vec.size(); ++i) {
    // int r, l;
    // tie(r, l) = vec[i];
    int r = vec[i].first, l = vec[i].second; // 替代结构化绑定
    ans[i] = bit.query(l) + 1;
    bit.add(l, ans[i]);
  }

  for (int i = 0; i < vec.size(); ++i) {
    int r = vec[i].first, l = vec[i].second; // 替代结构化绑定
    ans[i] += bit.query(r);
  }

  int res = *max_element(ans, ans + n);
  printf("%d\n", res);
  return 0;
}
